<%@page import="interface_layer.ErrorHandler"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP contains a represented list of all the courses that a specific administrator
	has created. It allows him to gather information about the courses, and manage (delete) them
	if he wishes.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Courses created by the admin</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
                            <li><a href="allCourses.jsp" class="current">Courses</a></li>
                    		<li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	// Need to add a session check here?
	String userType = (String) session.getAttribute("userType");
	
	if (null == username || null == userType)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	if (!userType.equals(Common.adminType))
	{
		/* Only an admin can enter here... */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);	
		return;
	}
	
	ArrayList<Course> adminCreatedCourses = QueryDispatcher.getAllGeneratedCourses(username);
	if (null == adminCreatedCourses)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	
	if (0 == adminCreatedCourses.size())
	{
%>
		<i>Sorry, you haven't created any courses yet, you can create a course <a href="newCourse.jsp">here</a></i>
<%
	}
	else
	{
%>
		<b><i>
		This list contains all courses you've created<br/>
		You may choose to <b>delete</b> each one of them.
		(You can always enter the course details and delete it)<br/>
		</i></b>

		<table border="1">
<%
		for (Course c : adminCreatedCourses)
		{
%>
			<tr>
				<td class="coursesAndUsersTable" id="coursesAndUsersDescription">	
<%
			out.println("<b>Name</b>: " + c.name + "<br/>");
			out.println("<b>Credit pts</b>: " + c.creditPoints + "<br/>");
			out.println("<b>Group number</b>: " + c.groupNumber + "<br/>");
			out.println("<a href='courseDetails.jsp?course=" + c.name + "'>More about this course</a>");
%>
				</td>
				<td class="coursesAndUsersTable" id="coursesAndUsersManagement">
		
					<form method="get" action="removeCourse">
					<input type="hidden" name="courseToRemove" value="<%= c.name %>"/>
					<input type="submit" value="Remove the course" />	
					</form>
				</td>
			</tr>
<%
		}
%>
		</table>
<%
	}
%>
	
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>